package jonevanfinalproject;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.ChangeEvent;

class GUIFrame extends JFrame implements ActionListener {

    JTextArea jta;
    JFrame Frame = new JFrame();
    GUIPanel panel = new GUIPanel();
    JPanel centerPanel = new JPanel();
    JPanel northPanel = new JPanel();
    ArrayList<Integer> currentPattern = new ArrayList();
    ArrayList<Integer> winningPattern = new ArrayList();
    ArrayList<JButton> doors = new ArrayList();
    JTextArea jta1;
    JButton playButton = new JButton("Play");
    JButton exitButton = new JButton("Exit");

    GUIFrame() {

        centerPanel.setVisible(false);

        int doorNum = 2;
        
        winningPattern.add(1);//not random
        int num = (int) (Math.random() * doorNum); 
        winningPattern.add(num); 
        doorNum++;
        int num1 = (int) (Math.random() *doorNum) + 0; 
        winningPattern.add(num1);//1-3
        doorNum++;
        int num2 = (int) (Math.random() *doorNum) + 0; 
        winningPattern.add(num2);//1-4
        doorNum++;
        int num3 = (int) (Math.random() *doorNum) + 0; 
        winningPattern.add(num3);//1-5
        doorNum++;
        int num4 = (int) (Math.random() *doorNum) + 0; 
        winningPattern.add(num4);//1-6
        doorNum++;
        int num5 = (int) (Math.random() *doorNum) + 0; 
        winningPattern.add(num5);//1-7
        doorNum++;
        int num6 = (int) (Math.random() *doorNum) + 0; 
        winningPattern.add(num6);//1-8
        doorNum++;
        int num7 = (int) (Math.random() *doorNum) + 0; 
        winningPattern.add(num7);//1-9
        doorNum++;
        int num8 = (int) (Math.random() *doorNum) + 0; 
        winningPattern.add(num8);//1-10
        doorNum++;
        
        centerPanel.setLayout(new GridLayout());

        this.add(centerPanel, BorderLayout.CENTER);

        for (int i = 0; i < 10; i++) {
            JButton b = new JButton("Door " + (i + 1));
            b.addActionListener(this);
            centerPanel.add(b);
            doors.add(b);
            
            
        }
       
        
        centerPanel.remove(doors.get(2));
        centerPanel.remove(doors.get(3));
        centerPanel.remove(doors.get(4));
        centerPanel.remove(doors.get(5));
        centerPanel.remove(doors.get(6));
        centerPanel.remove(doors.get(7));
        centerPanel.remove(doors.get(8));
        centerPanel.remove(doors.get(9));

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1000, 850);
        setVisible(true);
        setResizable(false);

        jta1 = new JTextArea("Welcome To The Door Game\n2 Doors: wow you pressed play!\n3 Doors: Ok\n4 Doors: Wow!\n5 Doors: Awesome!\n6 Doors: you're a Boss\n7 Doors: hacker\n8 Doors: stop hacking\n9 Doors: SERIOUSLY STOP HACKING\n10 Doors: GOD");

        Frame.setLayout(new GridBagLayout());
        this.add(northPanel, BorderLayout.NORTH);

        Frame.add(jta1);

        playButton.addActionListener(this);
        exitButton.addActionListener(this);
        Frame.add(playButton);
        Frame.add(exitButton);

        Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Frame.setSize(1000, 850);
        Frame.setVisible(true);
        Frame.setResizable(false);

    }

    @Override
    public void actionPerformed(ActionEvent ae) {


        if (ae.getActionCommand().equals("Door 1")) {
            currentPattern.add(0);
        } else if (ae.getActionCommand().equals("Door 2")) {
            currentPattern.add(1);
        } else if (ae.getActionCommand().equals("Door 3")) {
            currentPattern.add(2);
        } else if (ae.getActionCommand().equals("Door 4")) {
            currentPattern.add(3);
        } else if (ae.getActionCommand().equals("Door 5")) {
            currentPattern.add(4);
        } else if (ae.getActionCommand().equals("Door 6")) {
            currentPattern.add(5);
        } else if (ae.getActionCommand().equals("Door 7")) {
            currentPattern.add(6);
        } else if (ae.getActionCommand().equals("Door 8")) {
            currentPattern.add(7);
        } else if (ae.getActionCommand().equals("Door 9")) {
            currentPattern.add(8);
        } else if (ae.getActionCommand().equals("Door 10")) {
            currentPattern.add(9);
        }



        if (currentPattern.size() == 0) {

            centerPanel.add(doors.get(0));
            centerPanel.add(doors.get(1));

        } else if (currentPattern.size() == 1) {
            
            centerPanel.add(doors.get(0));
            centerPanel.add(doors.get(1));

        } else if (currentPattern.size() == 2) {
            
            centerPanel.add(doors.get(0));
            centerPanel.add(doors.get(1));
            centerPanel.add(doors.get(2));

        } else if (currentPattern.size() == 3) {
            
            centerPanel.add(doors.get(0));
            centerPanel.add(doors.get(1));
            centerPanel.add(doors.get(2));
            centerPanel.add(doors.get(3));

        } else if (currentPattern.size() == 4) {

            centerPanel.add(doors.get(0));
            centerPanel.add(doors.get(1));
            centerPanel.add(doors.get(2));
            centerPanel.add(doors.get(3));
            centerPanel.add(doors.get(4));

        } else if (currentPattern.size() == 5) {

            centerPanel.add(doors.get(0));
            centerPanel.add(doors.get(1));
            centerPanel.add(doors.get(2));
            centerPanel.add(doors.get(3));
            centerPanel.add(doors.get(4));
            centerPanel.add(doors.get(5));

        } else if (currentPattern.size() == 6) {

            centerPanel.add(doors.get(0));
            centerPanel.add(doors.get(1));
            centerPanel.add(doors.get(2));
            centerPanel.add(doors.get(3));
            centerPanel.add(doors.get(4));
            centerPanel.add(doors.get(5));
            centerPanel.add(doors.get(6));

        } else if (currentPattern.size() == 7) {
            
            centerPanel.add(doors.get(0));
            centerPanel.add(doors.get(1));
            centerPanel.add(doors.get(2));
            centerPanel.add(doors.get(3));
            centerPanel.add(doors.get(4));
            centerPanel.add(doors.get(5));
            centerPanel.add(doors.get(6));
            centerPanel.add(doors.get(7));

        } else if (currentPattern.size() == 8) {

            centerPanel.add(doors.get(0));
            centerPanel.add(doors.get(1));
            centerPanel.add(doors.get(2));
            centerPanel.add(doors.get(3));
            centerPanel.add(doors.get(4));
            centerPanel.add(doors.get(5));
            centerPanel.add(doors.get(6));
            centerPanel.add(doors.get(7));
            centerPanel.add(doors.get(8));
           
        } else if (currentPattern.size() == 9) {
            
            centerPanel.add(doors.get(0));
            centerPanel.add(doors.get(1));
            centerPanel.add(doors.get(2));
            centerPanel.add(doors.get(3));
            centerPanel.add(doors.get(4));
            centerPanel.add(doors.get(5));
            centerPanel.add(doors.get(6));
            centerPanel.add(doors.get(7));
            centerPanel.add(doors.get(8));
            centerPanel.add(doors.get(9));

        }
        this.validate();

        for (int i = 0; i < currentPattern.size(); i++) {
            if (currentPattern.get(i) != winningPattern.get(i)) {
                System.out.println("you lose!");
                System.exit(0);
            }
        }
        if (currentPattern.size() == winningPattern.size()) {
            System.out.println("you win!");
            System.exit(0);
        }
        if (ae.getActionCommand().equals("Exit")) {
            System.exit(0);
        }
        if (ae.getActionCommand().equals("Play")) {
            currentPattern.add(1);
            Frame.remove(playButton);
            Frame.remove(exitButton);
            Frame.dispose();
            centerPanel.setVisible(true);

        }
    }
}
